package cn.xt.demo;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.xt.demo.config.NetConfig;
import cn.xt.demo.utils.Md5Util;
import sun.net.www.http.HttpClient;

/**
 * Servlet implementation class AddAccount
 */

public class AddAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddAccount() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 try {  
		request.setCharacterEncoding("UTF-8");
		
		
		String appAccountId = request.getParameter("appAccountId");
		   long FY_TIMEINMILLIS = Calendar.getInstance().getTimeInMillis();//实际开发 不能用 static 不然值是固定的 
		   String FY_AU = Md5Util.md532upper(NetConfig.FY_APPID + NetConfig.FY_APPTOKEN + FY_TIMEINMILLIS);//MD5（FY_APPID+ FY_APPTOKEN+ti）
	
		String mynet2FyRequestString="?appId="+NetConfig.FY_APPID+"&appAccountId="+appAccountId+"&ti="+FY_TIMEINMILLIS+"&ver="+NetConfig.FY_API_VERSION+"&au="+FY_AU;
		
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		String urlString = "http://api.feiyucloud.com/api/addAccount"+mynet2FyRequestString;  
		
        String res = "";     
          
            URL url = new URL(urlString);    
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();    
            conn.setDoOutput(true);    
            conn.setRequestMethod("POST");   
            InputStream inputStream = conn.getInputStream();
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream,"UTF-8"));    
            String line; 
           while ((line = in.readLine()) != null) {    
               res += line+"\n";    
           }    
            in.close();    
        	writer.write(res);
    		writer.flush();
    		writer.close();
        } catch (Exception e) {    
        	e.printStackTrace();
            System.out.println("error in wapaction,and e is " + e.getMessage());    
        }
	
		
		
	}

}
